﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain.Models;
using Casanova.Web;
using Web;

namespace WebApp
{
    public partial class TicketManagement : System.Web.UI.Page
    {
        #region fields

        private int totalRecords;
        private OrderDirection orderDirection;
        private RepairingTicketOrderBy orderBy;
        private int success;
        private string redirectUrl = "AdminManagement.aspx";
        private string errorIds;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void GetParams()
        {
            orderBy = QueryStringUtility.GetEnum<RepairingTicketOrderBy>("OrderBy", RepairingTicketOrderBy.ID.ToString());
            orderDirection = QueryStringUtility.GetEnum<OrderDirection>("OrderDirection", OrderDirection.ASC.ToString());
            success = QueryStringUtility.GetInt32("success", 0);
            errorIds = QueryStringUtility.GetString("idserror", "");
        }

        private void LoadData()
        {
            
        }

        private void UpdateView()
        {
            (Master as SiteMaster).SelectedTab = "adminManagement";
            pageTitle.Text = Resources.Strings.RepairingManagementSystem;
            pager.TotalRecords = totalRecords;
            pager.PageSize = MyConfig.PageSize;

            messageLabel.Visible = success != 0;

            switch (success)
            {
                case 1:
                    //messageLabel.Text = Resources.Strings.AddSuccess;
                    break;
                case 2:
                    //messageLabel.Text = Resources.Strings.UpdateSuccess;
                    break;
                case 3:
                    //messageLabel.Text = Resources.Strings.DeleteSuccess;
                    break;
                case -1:
                    //messageLabel.Text = Resources.Strings.AddUnSuccess;
                    break;
                case -2:
                    //messageLabel.Text = Resources.Strings.UpdateUnSuccess;
                    break;
                case -3:
                    //messageLabel.Text = string.Format(Resources.Strings.DeleteUnSuccess, errorIds);
                    break;
            }
        }

        #region public functions

        public string GetOrderByLink(string propertyName)
        {
            OrderDirection newOrderDirection = orderDirection;
            string url = CreateUrlParams();
            if (propertyName == orderBy.ToString())
            {
                newOrderDirection = orderDirection == OrderDirection.ASC ? OrderDirection.DESC : OrderDirection.ASC;
            }
            return String.Format("{0}{4}OrderBy={1}&OrderDirection={2}&page={3}", url,
                propertyName, newOrderDirection.ToString(), pager.PageIndex, url.IndexOf('?') > 0 ? "&" : "?");
        }

        private string CreateUrlParams()
        {
            return redirectUrl;
        }

        public string GetOrderDirectionIndicator(string propertyName)
        {
            if (propertyName == orderBy.ToString())
            {
                return String.Format("<img src=\"../images/{0}.gif\" alt=\"{0}\" align=\"absmiddle\" />", orderDirection);
            }
            return "";
        }

        #endregion
    }
}